//
//  File.swift
//  
//
//  Created by WeCloudIOS on 2022/4/13.
//

import Foundation

import UIKit

class WCVideoView: UIView {
    
    var coverUrl:String?
    
    //计算属性
    var videoData: VideoTrack{
        set {
            remoteVideoView.track = newValue
        }
        
        get {
            return remoteVideoView.track!
        }
    }
    
    //计算属性
    var userInfo: String{
        
        get {
            return coverUrl!
        }

        set {
            
            if coverUrl != nil{
                self.coverImgage.dowloadFromServer(link:coverUrl! )
                return
            }
            
            if newValue == ""{
                return
            }
            
            let params:NSMutableDictionary = NSMutableDictionary()
            params["userId"] = newValue
            
            NetWork(url: "/api/user/getUserByUserId", params: params) { obj in
                let data: NSDictionary = obj as! NSDictionary
                
                DispatchQueue.main.async {
                    self.nameLabel.text = data.object(forKey: "nickname") as? String
                    
                    if data.object(forKey: "headPortrait") != nil{
                        
                        let link:String? = data.object(forKey: "headPortrait") as? String
                        if link != nil{
                            self.coverUrl = link
                            self.coverImgage.dowloadFromServer(link:link! )
                        }else{
                            self.coverImgage.image = Tool.SCImage(named: "firm_evaluation_avatar")
                        }
                        
                    }else{
                        print("用户头像为空")
                    }
                }   
            }
        }
    }
    
    //视图
    let remoteVideoView: VideoView = {
        let videoView = VideoView()
        videoView.backgroundColor = UIColor.black
        return videoView
    }()
    //用户头像
    let coverImgage:UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.backgroundColor = UIColor.gray
        return imageView
    }()
    
    
    //底部背景
    let nameBgView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.7)
        return bgView
    }()
    //用户名字
    let nameLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    let voiceImgage:UIImageView = {
        let imageView = UIImageView()
        imageView.isUserInteractionEnabled = true
        imageView.image = Tool.SCImage(named: "small_voice_close")
        return imageView
    }()
    
    //纯代码写
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black
        layer.masksToBounds = true
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUI(){
        addSubview(remoteVideoView)
        addSubview(coverImgage)
        addSubview(nameBgView)
        nameBgView.addSubview(nameLabel)
        nameBgView.addSubview(voiceImgage)
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        nameBgView.frame = CGRect(x: 0, y: frame.size.height-50, width: self.frame.size.width, height: 50)
        
        nameLabel.frame = CGRect(x: 10, y: 0, width: self.frame.size.width-60, height: 50)
        voiceImgage.frame = CGRect(x: self.frame.size.width-23, y: 13, width: 18, height: 24)
        remoteVideoView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        
        coverImgage.frame = remoteVideoView.frame
    }
}


class VideoSmallListView : UIView{
    //计算属性
    var userHeaderUrl: String{
        get {
            return sid!
        }

        set {
            self.coverImgage.dowloadFromServer(link:newValue)
        }
    }
    
    //计算属性
    var videoData: VideoTrack?{
        set {
            data = newValue!
            videoView.track = newValue!
        }
        
        get {
            return data
        }
    }
    
    var data: VideoTrack?
    
    let coverImgage:UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.backgroundColor = UIColor.gray
        return imageView
    }()
    
    let videoView = VideoView()
    var sid: String?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI(){
        
        videoView.backgroundColor = UIColor.black
        videoView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        coverImgage.frame = videoView.frame
        
        addSubview(videoView)
        addSubview(coverImgage)
        
    }
    
    
}
